﻿/*
 * Created by SharpDevelop.
 * User: amb
 * Date: 02/06/2014
 * Time: 04:15
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Windows.Forms;
using JdFormsDll;

namespace JdForms
{

    internal static class CSharpCompiler
    {
        internal static string ext = ".dll";

        internal static string Compile(string value, string filename)
        {
            try
            {
                if (!string.IsNullOrEmpty(value))
                {
                    CSharpCodeProvider codeProvider = new CSharpCodeProvider();
                    CompilerParameters parameters = new CompilerParameters();
                    parameters.GenerateInMemory = true;
                    parameters.GenerateExecutable = false;
                    parameters.TreatWarningsAsErrors = true;
                    parameters.WarningLevel = 4;
                    parameters.ReferencedAssemblies.Add("System.dll");
                    parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                    parameters.ReferencedAssemblies.Add("System.Drawing.dll");
                    parameters.ReferencedAssemblies.Add(@"C:\SVN\JdForms\JdFormsDll\bin\Debug\JdFormsDll.dll");
                    parameters.OutputAssembly = filename;
                    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, value);
                    if (results.Errors.HasErrors) throw new Exception();
                    return results.CompiledAssembly.FullName;
                }
                else return string.Empty;
            }
            catch { throw; }
        }
    }
}
